package com.geezdata.cps.sdk3rd.payment.pay.alipay;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradeCloseRequest;
import com.alipay.api.request.AlipayTradeFastpayRefundQueryRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.geezdata.cps.sdk3rd.payment.constant.Constants;
import com.geezdata.cps.sdk3rd.payment.pay.BasePay;
import com.geezdata.cps.sdk3rd.payment.request.CloseRequest;
import com.geezdata.cps.sdk3rd.payment.request.QueryRequest;
import com.geezdata.cps.sdk3rd.payment.request.RefundQueryRequest;
import com.geezdata.cps.sdk3rd.payment.request.RefundRequest;
import com.geezdata.cps.sdk3rd.payment.request.VerifyRequest;
import com.geezdata.cps.sdk3rd.payment.vo.PayConfigVO;

public abstract class AliPay extends BasePay {
	@Override
	public String close(CloseRequest request) {
		AlipayClient alipayClient = createAlipayClient(request.getPayConfig());
		
		String result = Constants.FAIL;
		try {
			result = alipayClient.execute(convert2AlipayCloseRequest(request)).getBody();
		} catch (AlipayApiException e) {
			e.printStackTrace();
		}
		
		return result;
	}

	@Override
	public String refund(RefundRequest request) {
		AlipayClient alipayClient = createAlipayClient(request.getPayConfig());
		
		String result = Constants.FAIL;
		try {
			result = alipayClient.execute(convert2AlipayRefundRequest(request)).getBody();
		} catch (AlipayApiException e) {
			e.printStackTrace();
		}

		return result;
	}

	@Override
	public String query(QueryRequest request) {
		AlipayClient alipayClient = createAlipayClient(request.getPayConfig());
		
		String result = Constants.FAIL;
		try {
			result = alipayClient.execute(convert2AlipayQueryRequest(request)).getBody();
		} catch (AlipayApiException e) {
			e.printStackTrace();
		}

		return result;
	}

	@Override
	public String refundQuery(RefundQueryRequest request) {
		AlipayClient alipayClient = createAlipayClient(request.getPayConfig());
		
		String result = Constants.FAIL;
		try {
			result = alipayClient.execute(convert2AlipayRefundQueryRequest(request)).getBody();
		} catch (AlipayApiException e) {
			e.printStackTrace();
		}

		return result;
	}
	
	@Override
	public boolean verify(VerifyRequest request) {
		try {
			return AlipaySignature.rsaCheckV1(request.getParams(), request.getPayConfig().getPayPublicKey(), 
					request.getPayConfig().getCharset(), request.getPayConfig().getSignType());
		} catch (AlipayApiException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	protected AlipayClient createAlipayClient(PayConfigVO payConfigVO) {
		return new DefaultAlipayClient(payConfigVO.getGatewayUrl(), payConfigVO.getAppId(), 
				payConfigVO.getMerchantPrivateKey(), "json", payConfigVO.getCharset(), 
				payConfigVO.getPayPublicKey(), payConfigVO.getSignType());
	}
	
	private AlipayTradeCloseRequest convert2AlipayCloseRequest(CloseRequest request) {
		JSONObject jsonContent = new JSONObject();
		jsonContent.put("out_trade_no", request.getOutTradeNo());
        String bizContent = jsonContent.toString().replaceAll("\"", "'");
        
        AlipayTradeCloseRequest alipayRequest = new AlipayTradeCloseRequest();
        alipayRequest.setBizContent(bizContent);
        
		return alipayRequest;
	}
	
	private AlipayTradeRefundRequest convert2AlipayRefundRequest(RefundRequest request) {
		JSONObject jsonContent = new JSONObject();
		jsonContent.put("out_trade_no", request.getOutTradeNo());
		jsonContent.put("refund_amount", request.getRefundAmount());
		jsonContent.put("refund_reason", request.getRefundReason());
		jsonContent.put("out_request_no", request.getOutRequestNo());
        String bizContent = jsonContent.toString().replaceAll("\"", "'");
        
        AlipayTradeRefundRequest alipayRequest = new AlipayTradeRefundRequest();
        alipayRequest.setBizContent(bizContent);
       
        return alipayRequest;
	}
	
	private AlipayTradeQueryRequest convert2AlipayQueryRequest(QueryRequest request) {
		JSONObject jsonContent = new JSONObject();
		jsonContent.put("out_trade_no", request.getOutTradeNo());
        String bizContent = jsonContent.toString().replaceAll("\"", "'");
        
        AlipayTradeQueryRequest alipayRequest = new AlipayTradeQueryRequest();
        alipayRequest.setBizContent(bizContent);
        
        return alipayRequest;
	}
	
	private AlipayTradeFastpayRefundQueryRequest convert2AlipayRefundQueryRequest(RefundQueryRequest request) {
		JSONObject jsonContent = new JSONObject();
		jsonContent.put("out_trade_no", request.getOutTradeNo());
		jsonContent.put("out_request_no", request.getOutRequestNo());
        String bizContent = jsonContent.toString().replaceAll("\"", "'");
        
		AlipayTradeFastpayRefundQueryRequest alipayRequest = new AlipayTradeFastpayRefundQueryRequest();
		alipayRequest.setBizContent(bizContent);
        
        return alipayRequest;
	}
}
